package cn.yunhe.springmvc.web;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.yunhe.springmvc.model.User;

/**
 * 用户控制器
 * 
 * @author YUNHE
 *
 */
@Controller
public class UserController {
	
	/**
	 * 声明用户集合
	 */
	private Map<String,User> userMap = new HashMap<String,User>();
	
	/**
	 * 构造函数模拟用户集合数据
	 */
	public UserController(){
	    userMap.put("zhangsan", new User("zhangsan", "123"));  
        userMap.put("lishimin", new User("lishimin", "456")); 
	}
	
	@RequestMapping("/index1")
	public String index1(){
		return "index";
	}
	
	@RequestMapping("/index2")
	public ModelAndView index2(){
		return new ModelAndView("index");
	}
	
	@RequestMapping("/home1")
	public String home1(Model model){
		if(model.containsAttribute("ceshi")){
			System.out.println("有ceshi属性");
		}
		model.addAttribute("username", "张三1");
		return "user";
	}
	
	@RequestMapping("/home11")
	public ModelAndView home11(Model model){
		model.addAttribute("username", "张三11");
		return new ModelAndView("user");
	}
	
	@RequestMapping("/home2")
	public String home2(ModelMap model){
		model.addAttribute("username", "李四2");
		return "user";
	}
	
	@RequestMapping("/home22")
	public ModelAndView home22(ModelMap model){
		model.addAttribute("username", "李四22");
		return new ModelAndView("user");
	}
	
	@RequestMapping("/home3")
	public ModelAndView home3(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user");
		Map<String,Object> csmap = new HashMap<String,Object>();
		csmap.put("username", "王五3");
		mv.addAllObjects(csmap);
		return mv;
	}
	
	@RequestMapping("/home33")
	public ModelAndView home33(){
		Map<String,Object> csmap = new HashMap<String,Object>();
		csmap.put("username", "王五33");
		return new ModelAndView("user",csmap);
	}
	
	@RequestMapping("/home4")
	public ModelAndView home4(@ModelAttribute("username") String username){

		return new ModelAndView("user");
	}
	
	@RequestMapping("/home44")
	public ModelAndView home44(String username,Model model){
		System.out.println("username:"+username);
		model.addAttribute("username", username);
		return new ModelAndView("user");
	}
	
	
	@RequestMapping("/home5")
	public ModelAndView home5(HttpServletRequest request){
		
		String username = request.getParameter("username");
		System.out.println("username:"+username);

		return new ModelAndView("user");
	}
	
}
